package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.entity.XiaoClass;
import com.net.tkc.pro.impl.XiaoClassImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/Class/")
public class XiaoClassController {
    ResultEntity resultEntity = null;
    @Resource
    XiaoClassImpl aClass;

    /**
     * 添加小课堂信息
     * @param picture 图片
     * @param title 标题
     * @param author 作者
     * @return
     */
    @PostMapping("Class")
    private String addClass(
            @RequestParam("picture") String picture,
            @RequestParam("title") String title,
            @RequestParam("author") String author
    ){
        resultEntity = new ResultEntity();
        XiaoClass xiaoClass=new XiaoClass();
        xiaoClass.setPicture(picture);
        xiaoClass.setTitle(title);
        xiaoClass.setAuthor(author);
        try{
            int result =aClass.addXiaoClass(xiaoClass);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 根据id来通过审核
     * @param examine
     * @param id
     * @return
     */
    @PutMapping("UpE")
    private String upExamine(
            @RequestParam("examine") int examine,
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = aClass.upExamine(examine,id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 根据id更新用户状态
     * @param status
     * @param id
     * @return
     */
    @PutMapping("UpS")
    private String upstatus(
            @RequestParam("status") int status,
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = aClass.upstatus(status,id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 无条件查询Class里面的数据
     * @return
     */
    @GetMapping("Class")
    private String getClassList(
    ){
        resultEntity = new ResultEntity();
        try{
            List<XiaoClass> list =aClass.getClassList();
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     *  根据作者查找帖子
     * @param title 作者
     * @return
     */
    @GetMapping("GetTitle")
    private String getTitleList(
            @RequestParam("title") String title
    ) {
        resultEntity = new ResultEntity();
        try {
            if(!title.equals("") || title !=null){
                title="%"+title+"%";
            }
            List<XiaoClass> list = aClass.getTitleList(title);
            if (list.size() != 0) {
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            } else {
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     *  根据作者查找帖子
     * @param author 作者
     * @return
     */
    @GetMapping("GetAuthor")
    private String getAuthorList(
            @RequestParam("author") String author
    ){
        resultEntity = new ResultEntity();
        try{
            if(!author.equals("") || author !=null){
                author="%"+author+"%";
            }
            List<XiaoClass> list=aClass.getAuthorList(author);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
